package org.example.openhtmltopdf;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ResourceLoader;
import java.io.File;
import java.io.IOException;


@SpringBootApplication
public class OpenhtmltopdfApplication {

    public static void main(String[] args) {
        SpringApplication.run(OpenhtmltopdfApplication.class, args);
    }

    @Bean("config")
    public Configuration config(ResourceLoader loader) {
        Configuration cfg = null;
        try {
            cfg = new Configuration(Configuration.VERSION_2_3_33);
            // 设置模板路径
            File baseDir = loader.getResource("classpath:/templates/").getFile();
            TemplateLoader templateLoader = new FileTemplateLoader(baseDir);
            cfg.setTemplateLoader(templateLoader);
            // 设置编码
            cfg.setDefaultEncoding("UTF-8");
            cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return cfg;
    }
}
